package com.hzjh.h5mall.ui.register

import android.graphics.Paint
import android.os.Bundle
import android.widget.TextView
import androidx.recyclerview.widget.GridLayoutManager
import com.alibaba.android.arouter.facade.annotation.Autowired
import com.alibaba.android.arouter.facade.annotation.Route
import com.hzjh.base.ext.R
import com.hzjh.base.ext.frame.JHBaseActivity
import com.hzjh.base.ext.route.RouteUrl
import com.hzjh.base.ext.utils.TextUtils
import com.hzjh.base.ext.widget.balloon.CommonTipBalloon2
import com.hzjh.base.frame.adapter.MutableAdapter
import com.hzjh.base.frame.adapter.SimpleGapItemDecor
import com.hzjh.base.router.ext.navigationTo
import com.hzjh.base.style.weight.ToastKt.toastCenter
import com.hzjh.base.utils.function.dp2px
import com.hzjh.base.utils.function.visibleOrGone
import com.hzjh.base.utils.function.visibleOrInVisible
import com.hzjh.base.utils.other.CheckUtils
import com.hzjh.base.utils.other.CommUtils
import com.hzjh.h5mall.adapter.register.MnemonicAdapter
import com.hzjh.h5mall.databinding.ActivityMnemonicBinding
import com.hzjh.h5mall.databinding.ItemMnemonicBinding
import com.hzjh.h5mall.model.register.RegisterVM
import com.skydoves.balloon.balloon

//助记词生成页面
@Route(path = RouteUrl.MNEMONIC_ACTIVITY)
class MnemonicActivity : JHBaseActivity<ActivityMnemonicBinding, RegisterVM>() {
    @JvmField
    @Autowired(name = RouteUrl.PARAMS_EMAIL)
    var email = ""


    private var ifHaveMnemonic = false
    private var ifShowMnemonic = true
    private val balloon by balloon<CommonTipBalloon2>()
    private val  mnemonicAdapter by lazy {
        MutableAdapter(viewModel.mnemonicList.value!!).apply{
            addVhDelegate {
                MnemonicAdapter(ItemMnemonicBinding.inflate(layoutInflater, it, false))
            }
        }
    }
    override fun initData() {
        viewModel.initMnemonic()
    }
    override fun initView() {
        viewBinding.rvMnemonic.layoutManager = GridLayoutManager(this,3)
        viewBinding.rvMnemonic.addItemDecoration(
            SimpleGapItemDecor( verGap = 12.dp2px())
        )
        viewBinding.rvMnemonic.adapter = mnemonicAdapter
        TextUtils.addTextIcon(this,viewBinding.tvRecoveryTip,R.drawable.svg_icon_help){
            //弹气泡提示
            balloon.showAlignBottom(it)
            balloon.getContentView().apply {
                findViewById<TextView>(R.id.tv_message).text = CommUtils.getString(R.string.jh_generate_tip4)
            }
        }
        viewBinding.tvCopyGenerate.apply {
            paint.flags = Paint.UNDERLINE_TEXT_FLAG; //下划线
            paint.isAntiAlias = true;//抗锯齿
            setOnClickListener {
                //复制助记词
                val text = viewModel.mnemonicList.value?.joinToString(" ") { it.name }
                CommUtils.copyText(text)
                toastCenter(CommUtils.getString(R.string.jh_copy_succeed))
            }
        }
        viewBinding.llShowState.setOnClickListener {
            //显示隐藏助记词
            ifShowMnemonic=!ifShowMnemonic
            viewBinding.rvMnemonic.visibleOrInVisible(ifShowMnemonic)
            viewBinding.tvShowTitle.text = if(ifShowMnemonic) CommUtils.getString(R.string.jh_displayed) else CommUtils.getString(R.string.jh_hidden)
            viewBinding.ivYeaState.setImageResource(if(ifShowMnemonic) R.drawable.svg_yea_show else R.drawable.svg_eye_close)
        }


        viewBinding.btnMnemonic.setOnClickListener {
            if(ifHaveMnemonic){
                //已生成助记词 下一步
                navigationTo(RouteUrl.MNEMONIC_CHECK_ACTIVITY){
                    withBundle(RouteUrl.PARAMS_MNEMONIC_WORD, Bundle().apply {
                        putSerializable("mnemonicList",viewModel.mnemonicList.value)
                    })
                    withString(RouteUrl.PARAMS_EMAIL,email)
                }
            }else{
                //未生成助记词 生成助记词
                viewModel.reqCreateMnemonic(email)
            }
        }
    }

    override fun initObservable() {
        super.initObservable()
        viewModel.mnemonicList.observe(this){
            mnemonicAdapter.notifyDataSetChanged()
            changeState()
        }
    }

    //助记词生成状态
    private fun changeState(){
        ifHaveMnemonic = true
        viewModel.mnemonicList.value?.map {
            if(CheckUtils.isEmpty(it.name)){
                //助记词生成失败
                ifHaveMnemonic = false
            }
        }
        viewBinding.llGenerateCreated.visibleOrGone(ifHaveMnemonic){
            viewBinding.llGenerateNoCreated.visibleOrGone(false)
            viewBinding.btnMnemonic.text = CommUtils.getString(R.string.jh_next)
        }
    }

    override fun initViewBinding(): ActivityMnemonicBinding {
        return ActivityMnemonicBinding.inflate(layoutInflater)
    }
}